const app = getApp();
import services from '../../services/clockin';
import common from '../../utils/common';
import moment from 'moment';

Page({
  data: {
    isReady: false,
    isLoading: false,
    unionid: '',
    mode: 0,
    modeOptions: [{
        title: '导出工地报表'
      },
      {
        title: '导出员工报表'
      },
      {
        title: '导出打卡明细'
      }
    ],
    formData1: {
      exportTime: [], // 日期
      stations: [], // 工地
      email: '',
    },
    formData2: {
      exportTime: '', // 日期
      users: [], // 员工
      email: '',
    },
    formData3: {
      exportTime: '', // 日期
      email: '',
    },
    stationList: [],
    userList: [],
  },
  async onLoad() {
    const {
      unionid
    } = app.globalData.userInfo || {};
    this.setData({
      unionid: unionid || '2QV8HeciS9zgYhRAtxbpiPZwiEiE'
    });
    await this.initData();
    this.setData({
      isReady: true
    });
  },
  async onShow() {
    if (!this.data.isReady) {
      await this.initData();
    }
  },
  async initData() {
    await this.queryGetStation();
    await this.queryGetUserList();
  },
  async queryGetStation() {
    try {
      const res = await services.getStation(this.data.unionid);
      if (res && res.data && res.data.length) {
        const stationList = res.data.map((item) => {
          const {
            name,
            id
          } = item || {};
          return {
            text: name,
            value: id
          };
        });
        this.setData({
          stationList,
        });
        console.log('stationList', this.data);
      }
    } catch (error) {
      console.log('获取补卡列表失败', error);
    }
  },
  async queryGetUserList() {
    try {
      const res = await services.getUserList(this.data.unionid);
      if (res && res.data && res.data.length) {
        const userList = res.data.map((item) => {
          const {
            name,
            unionid
          } = item || {};
          return {
            text: name,
            value: unionid
          };
        });
        this.setData({
          userList,
        });
        console.log('userList', this.data);
      }
    } catch (error) {
      console.log('获取补卡列表失败', error);
    }
  },
  handleExport() {
    if (this.data.mode === 0) {
      this.queryPostExportStationReport();
    } else if (this.data.mode === 1) {
      this.queryPostExportUserReport();
    } else {
      this.queryPostExportUserDetailReport();
    }
  },
  handleChangeMode(current) {
    this.setData({
      mode: current || 0
    });
  },
  handleChangeStations(value) {
    console.log('handleChangeStations', value);
    this.setData({
      formData1: {
        ...this.data.formData1,
        stations: value
      }
    });
  },
  handleChangeUsers(value) {
    console.log('handleChangeUsers', value);
    this.setData({
      formData2: {
        ...this.data.formData2,
        users: value
      }
    });
  },
  handleChangeEmail(value) {
    console.log('handleChangeEmail', value);
    if (this.data.mode === 0) {
      this.setData({
        formData1: {
          ...this.data.formData1,
          email: value.detail.value
        }
      });
    } else if (this.data.mode === 1) {
      this.setData({
        formData2: {
          ...this.data.formData2,
          email: value.detail.value
        }
      });
    } else {
      this.setData({
        formData3: {
          ...this.data.formData3,
          email: value.detail.value
        }
      });
    }
  },
  handleRangeExportTime(value) {
    console.log('handleRangeExportTime', value);
    if (this.data.mode === 0) {
      this.setData({
        formData1: {
          ...this.data.formData1,
          exportTime: value
        }
      });
      return
    }
    if (this.data.mode === 1) {
      this.setData({
        formData2: {
          ...this.data.formData2,
          exportTime: value
        }
      });
      return
    }
    this.setData({
      formData3: {
        ...this.data.formData3,
        exportTime: value
      }
    });
  },
  handleChangeDay(value) {
    console.log('handleChangeDay', value);
    if (this.data.mode === 0) {
      this.setData({
        formData1: {
          ...this.data.formData1,
          statistic_day: value
        }
      });
    } else if (this.data.mode === 1) {
      this.setData({
        formData2: {
          ...this.data.formData2,
          statistic_day: value
        }
      });
    } else {
      this.setData({
        formData3: {
          ...this.data.formData3,
          statistic_day: value
        }
      });
    }
  },
  async queryPostExportUserDetailReport() {
    const {
      exportTime,
      email,
    } = this.data.formData3;
    console.log('queryPostExportUserReport', exportTime, email);
    if (!exportTime) {
      dd.showToast({
        type: 'info',
        content: '请选择日期范围',
        duration: 1000
      });
      return;
    }
    if (!email) {
      dd.showToast({
        type: 'info',
        content: '请填写邮箱',
        duration: 1000
      });
      return;
    }
    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true
    });
    try {
      const params = {
        receiver_email: email || '',
        "year": moment(exportTime).format('YYYY'),
        "month": moment(exportTime).format('MM'),
      };
      const res = await services.postExportUserDetailReport(params, this.data.unionid);
      console.log('postExportUserReport', this.formData3, params, res);
      dd.showToast({
        type: 'success',
        content: '导出成功',
        duration: 2000
      });
    } catch (error) {
      console.log('error', error);
      common.showErrorToast(error, error.data);
    } finally {
      this.setData({
        isLoading: false
      });
    }
  },
  async queryPostExportUserReport() {
    const {
      exportTime,
      users,
      email,
    } = this.data.formData2;
    console.log('queryPostExportUserReport', exportTime, users, email);
    if (!exportTime) {
      dd.showToast({
        type: 'info',
        content: '请选择日期范围',
        duration: 1000
      });
      return;
    }
    if (!email) {
      dd.showToast({
        type: 'info',
        content: '请填写邮箱',
        duration: 1000
      });
      return;
    }
    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true
    });
    try {
      const params = {
        receiver_email: email || '',
        "year": moment(exportTime).format('YYYY'),
        "month": moment(exportTime).format('MM'),
      };
      if (users && users.length) {
        params.unionid_list = users
      }
      const res = await services.postExportUserReport(params, this.data.unionid);
      console.log('postExportUserReport', this.formData2, params, res);
      dd.showToast({
        type: 'success',
        content: '导出成功',
        duration: 2000
      });
    } catch (error) {
      console.log('error', error);
      common.showErrorToast(error, error.data);
    } finally {
      this.setData({
        isLoading: false
      });
    }
  },
  async queryPostExportStationReport() {
    const {
      exportTime, // 日期
      stations, // 工地
      email,
    } = this.data.formData1;
    console.log('queryPostExportStationReport', exportTime, stations, email);
    if (!exportTime || exportTime.length !== 2) {
      dd.showToast({
        type: 'info',
        content: '请选择日期范围',
        duration: 1000
      });
      return;
    }
    const dg = moment(exportTime[1]).diff(moment(exportTime[0]), 'day')
    if (dg > 60) {
      dd.showToast({
        type: 'info',
        content: '日期范围不可超过60天',
        duration: 1000
      });
      return;
    }
    if (!email) {
      dd.showToast({
        type: 'info',
        content: '请填写邮箱',
        duration: 1000
      });
      return;
    }
    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true
    });
    try {
      const params = {
        receiver_email: email || '',
        start_date: moment(exportTime[0]).format('YYYY-MM-DD'),
        end_date: moment(exportTime[1]).format('YYYY-MM-DD')
      };
      if (stations && stations.length) {
        params.station_id_list = stations
      }
      const res = await services.postExportStationReport(params, this.data.unionid);
      console.log('queryPostExportStationReport', this.formData1, params, res);
      dd.showToast({
        type: 'success',
        content: '导出成功',
        duration: 2000
      });
    } catch (error) {
      console.log('error', error);
      common.showErrorToast(error, error.data);
    } finally {
      this.setData({
        isLoading: false
      });
    }
  },
});